/*
 * @Author: HeAo
 * @Date: 2021-09-29 14:16:03
 * @LastEditTime: 2021-09-29 14:32:25
 * @LastEditors: HeAo
 * @Description:
 * @FilePath: \web2103-2105\web2105\3-1-ES6\day05\1-promise.js
 * 别乱动！
 */
const promise = new Promise((resolve, reject) => {
  if (1 > 0) {
    const response = {
      status: 200,
      message: '操作成功',
      data: null,
      timestamp: new Date().getTime()
    }
    // 异步请求成功，promise状态padding => fulfilled
    resolve(response)
  } else {
    const error = {
      status: 500,
      message: '后台接口异常',
      data: 'null',
      timestamp: new Date().getTime()
    }
    // 异步请求失败，promise状态padding => rejected
    reject(error)
  }
})

// promise直接调用.then .catch叫做链式调用
// promise状态pending => fulfilled =>被.then()捕获，其内的回调函数接替resolve()
promise
  .then((res) => {
    console.log(res);
  })
  // promise状态pending => rejected =>被.catch()捕获，其内的回调函数接替reject()
  .catch((err) => {
    console.log(err);
  })